<?php

namespace App\Admin\Controllers\Commodity;

use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use Illuminate\Http\Request;
use App\Model\Commodity_info;

class ApiInfoController extends Controller
{
    use ModelForm;

    public function get_info(Request $request)
    {
        $data = $request->input();
        if (empty($data) || empty($data["commodity_id"]))
            return return_array(1, '非法参数');
        $info = new Commodity_info();
        if (!empty($info = $info->get_info_by_commodity_id($data["commodity_id"])))
            return return_array(0, 'yes', $info);
        else
            return return_array(1, '未知错误');
    }

    public function add_info(Request $request)
    {
        $data = $request->input();
        if (
            empty($data) ||
            empty($data["up_img_url"]) ||
            empty($data["commodity_info"]) ||
            empty($data["color_list"]) ||
            empty($data["size_list"]) ||
            empty($data["commodityNumber"]) ||
            empty($data["commodityPrice"])
        )
            return return_array(1, '非法参数');

        $data = array(
            "info_img_url" => $data["up_img_url"],
            "commodity_id" => $data["commodity_info"],
            "commodity_color_id" => $data["color_list"],
            "commodity_size_id" => $data["size_list"],
            "commodity_info_number" => $data["commodityNumber"],
            "commodity_info_price" => $data["commodityPrice"],
            "commodity_classify_id" => ''
        );
        $info = new Commodity_info();
        if (!empty($info->add_info($data)))
            return return_array(0, 'yes', $info->get_info_by_commodity_id($data["commodity_id"]));
        else
            return return_array(1, '未知错误');
    }
}
